<script setup>
import { ref, defineProps, reactive } from 'vue'
import { _searchItem, _saveItem } from '/src/api/parameterConfiguration/index.js'
import { pushAllUrlData } from '../js/saveItem_all.js'
import { mainStore } from '/src/store/mainStore.js'

const store = mainStore()
const msg = defineProps({
  data: Object,
  height: Number,
  width: Number,
  boxWidth: Number,
})

const inputValue = ref([])
inputValue.value = msg.data.arr

const inputType = ref('') //显示类型
const children = ref([]) //下拉选择框的子选项
const width = ref(0)
const boxWidth = ref(0)
children.value = msg.data.children
inputType.value = msg.data.inputType
width.value = msg.width ? msg.width + 'px' : '100%'
boxWidth.value = msg.boxWidth ? msg.boxWidth + 'px' : '100%'

const editStatus = ref('') // 编辑状态 未保存为true 保存为faslse
const errTip = ref(null)

const verifyInput = (value) => {
  editStatus.value = '*'
  pushAllUrlData(msg.data)
}

// 搜索选项
const searchItem = async () => {
  const { parentNode, key } = msg.data

  const url = `${store.cutArgPage}${parentNode ? '/' + parentNode : ''}/${key}`
  const res = await _searchItem(url)
  if (res.data.code != 200) ElMessage.error('查询失败')

  ElMessage.success('查询成功')
  msg.data.arr = res.data.data[key]
  inputValue.value = msg.data.arr

  editStatus.value = ''
  errTip.value = null
}

// 保存选项
const saveItem = async () => {
  if (errTip.value != null) return ElMessage.error('保存失败，请输入正确的参数值')

  const { parentNode, key } = msg.data

  const params = {}
  params[key] = msg.data.arr
  if (parentNode) {
    params[parentNode] = {}
    params[parentNode][key] = msg.data.arr
  }

  const url = `${store.cutArgPage}`

  // 处理数据
  const res = await _saveItem(url, params)

  if (res.data.code != 200) ElMessage.error('保存失败')
  ElMessage.success('保存成功')
  msg.data.arr = parentNode ? res.data.data[parentNode][key] : res.data.data[key]

  editStatus.value = ''
  errTip.value = null
}
</script>

<template>
  <div class="inputEditerArr-container" v-if="inputType == 'text'">
    <div class="title">
      <p :class="editStatus ? 'editing' : ''" :style="{ width }">
        <span>{{ editStatus }}</span>
        {{ msg.data.name }}
      </p>

      <div class="box" :style="{ width: boxWidth }">
        <div v-for="(item, index) in inputValue">
          <el-input v-model.number="inputValue[index]" @input="verifyInput" />
          <span v-show="index != inputValue.length - 1">-</span>
        </div>
      </div>

      <img src="/public/parameterConfig/search.png" @click="searchItem" />
      <img src="/public/parameterConfig/save.png" @click="saveItem" />
    </div>
  </div>

  <div class="inputEditerArr-container inputEditerArr-container2" v-if="inputType == 'object'">
    <div class="title">
      <p :class="editStatus ? 'editing' : ''">
        <span>{{ editStatus }}</span>
        {{ msg.data.name }}
      </p>
      <img src="/public/parameterConfig/search.png" @click="searchItem" />
      <img src="/public/parameterConfig/save.png" @click="saveItem" />
    </div>

    <div class="main-box">
      <div class="input-item" v-for="(item, key, index) in inputValue">
        <p>{{ key }}</p>
        <el-input v-model.number="inputValue[key]" clearable @input="verifyInput" style="width: 500px" />
      </div>
    </div>
  </div>

  <div class="inputEditerArr-container inputEditerArr-container2" v-if="inputType == 'objectArr'">
    <div class="title">
      <p :class="editStatus ? 'editing' : ''">
        <span>{{ editStatus }}</span>
        {{ msg.data.name }}
      </p>
      <img src="/public/parameterConfig/search.png" @click="searchItem" />
      <img src="/public/parameterConfig/save.png" @click="saveItem" />
    </div>

    <div class="main-box">
      <div class="input-item" v-for="(item, key, index) in inputValue">
        <p>{{ key }}</p>
        <div v-for="(iten, index) in item">
          <el-input v-model.number="item[index]" @input="verifyInput" />
          <span v-show="index != item.length - 1">-</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.inputEditerArr-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  color: black;
  > p {
    line-height: 26px;
  }
  img {
    width: 20px;
    height: 20px;
    margin-left: 4px;
    cursor: pointer;
  }

  .box {
    // width: 00px;
    height: 32px;
    display: flex;
    align-items: center;
    padding: 2px 0;
    > div {
      display: flex;
      align-items: center;
    }
  }

  .editing {
    color: blue;
  }

  .title {
    display: flex;
    align-items: center;
    > img {
      margin-left: 10px;
      width: 20px;
      height: 20px;
    }
  }

  .errorTip {
    position: absolute;
    left: 0px;
    top: 26px;
    font-size: 10px;
    color: red;
    padding-left: 6px;
    width: 100%;
    opacity: 0.8;
  }
}

.inputEditerArr-container2 {
  height: 500px;
  border: 1px solid #ccc;
  .title {
    height: 30px;
    display: flex;
    align-items: center;
    > img {
      margin-top: 3px;
    }
  }
  .main-box {
    padding: 10px 20px;
  }
  .input-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    > p {
      width: 160px;
    }
    > div {
      width: 180px;
      display: flex;
      align-items: center;
    }
  }
}
</style>

<style lang="less">
.inputBox,
.selectBox {
  .el-input,
  .el-input--suffix {
    height: 26px;
  }
  .el-input__inner,
  .el-input__wrapper {
    height: 26px;
  }

  .el-select__wrapper {
    height: 26px;
    min-height: 26px;
  }
}

.arrBoxItem {
  .el-input,
  .el-input--suffix {
    height: 20px;
  }
  .el-input__inner,
  .el-input__wrapper {
    height: 20px;
  }

  .el-select__wrapper {
    height: 20px;
    min-height: 20px;
  }
}
</style>
